<?php

$this->breadcrumbs = array(
    'Comments' => array('index'),
    'Manage',
);

$this->menu = array(
    array('label' => 'List Comment', 'url' => array('index')),
    array('label' => 'Create Comment', 'url' => array('create')),
);


Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();

	return false;
});

$('.search-form form').submit(function(){
	$('#comment-grid').yiiGridView('update', {
		data: $(this).serialize()
	});

	return false;
});
");

?><div class="content-box-header">
    <h3>Quản lý Banner</h3>
    <ul class="content-box-tabs">
        <li><a href="#tab1" class="default-tab">Table</a></li>
        <!-- href must be unique and match the id of target div -->
        <li><a href="#tab2">Forms</a></li>
    </ul>
    <div class="clear"></div>
</div>
<!-- End .content-box-header -->
<div class="content-box-content">
    <div id="response"></div>
    <div class="tab-content default-tab" id="tab1">

<?php $grid = $this->widget('zii.widgets.grid.CGridView', array(
    'id'           => 'comment-grid',
    'dataProvider' => $model->search(),
    'filter'       => $model,
    'selectableRows' => 2,
    'selectionChanged' => "updateSelectors",
    'afterAjaxUpdate' => 'js:function(){
        $(".changeValue").bind("click change", function() {
            var orderId = $(this).attr("id").replace("changeValue_", "");
            var orderValue = $(this).val();
            var orderAttribute = $(this).attr("class").replace("changeValue ", "");
            var gridName = "' . Yii::app()->controller->id .'";
            var modelName = "' . ucfirst(Yii::app()->controller->id) .'";

            switch (orderAttribute) {
                case "page":
                    orderValue = $(this).is(":checked") == true ? 1 : 0;
                    break;
                case "status":
                    orderValue = $(this).attr("alt") == "active" ? 1 : 2;
                    break;
                case "is_read":
                    orderValue = $(this).attr("alt") == "active" ? 1 : 2;
                    break;
            }

            $.ajax({
                url:"' . Helper::url('/Admin/default/ajaxUpdate') . '",
                type: "post",
                data: { "id": orderId, "value": orderValue, "attr": orderAttribute, "model": modelName },
                success: function(){
                    $.fn.yiiGridView.update(gridName + "-grid");
                }
            });

            return false;
        })
    }',
    'columns'      => array(
        array(
            'class' => 'CCheckBoxColumn',
            'value' => '$data->id',
            'htmlOptions' => array('width' => '3%', 'class' => 'check-all'),
        ),
        'full_name',
        'website',
        'post_title',
        'parent_id',
        'info',
        array(
            'name'        => 'status',
            'value'       => '$data->status == 1 ? CHtml::image(Helper::themeUrl()."/images/log_severity1.gif","active", array("id" => "changeValue_$data->id", "class" => "changeValue status")) : CHtml::image(Helper::themeUrl()."/images/delicon.gif", "inactive", array("id" => "changeValue_$data->id", "class" => "changeValue status"))',
            'type'        => 'raw',
            'filter'      => Lookup::items('status'),
            'htmlOptions' => array('style' => 'text-align: center'),
        ),
        array(
            'name'        => 'is_read',
            'value'       => '$data->is_read == 1 ? CHtml::image(Helper::themeUrl()."/images/log_severity1.gif","active", array("id" => "changeValue_$data->id", "class" => "changeValue is_read")) : CHtml::image(Helper::themeUrl()."/images/delicon.gif", "inactive", array("id" => "changeValue_$data->id", "class" => "changeValue is_read"))',
            'type'        => 'raw',
            'filter'      => Lookup::items('Status'),
            'htmlOptions' => array('style' => 'text-align: center'),
        ),
        array(
            'name'  => 'create_date',
            'value' => 'Helper::getTimeAgo($data->create_date)'
        ),
        /*
        'id',
        'email',
        'is_spam',
        'post_id',
        'post_alias',
        'user_id',
        'owner_post',
        'update_date',
        'logo',
        */
        array(
            'class' => 'CButtonColumn',
        ),
    ),
)); ?>
    </div>
</div>

<?php
$script = '
    $(".changeValue").bind("click change", function() {
        var orderId = $(this).attr("id").replace("changeValue_", "");
        var orderValue = $(this).val();
        var orderAttribute = $(this).attr("class").replace("changeValue ", "");
        var gridName = "' . Yii::app()->controller->id .'";
        var modelName = "' . ucfirst(Yii::app()->controller->id) .'";

        switch (orderAttribute) {
            case "page":
                orderValue = $(this).is(":checked") == true ? 1 : 0;
                break;
            case "status":
                orderValue = $(this).attr("alt") == "active" ? 1 : 2;
                break;
            case "is_read":
                orderValue = $(this).attr("alt") == "active" ? 1 : 2;
                break;
        }

        $.ajax({
            url:"' . Helper::url('/Admin/default/ajaxUpdate') . '",
            type: "post",
            data: { "id": orderId, "value": orderValue, "attr": orderAttribute, "model": modelName },
            success: function(){
                $.fn.yiiGridView.update(gridName + "-grid");
            }
        });

        return false;
    })
';
Helper::cs()->registerScript('set-status', $script, CClientScript::POS_END);

if ($grid->dataProvider->ItemCount) {
    $this->menu[] = array(
        'label' => 'Delete selected items', 'url' => Helper::url('/Admin/default/deleteSelected'),
        'linkOptions' => array('onclick' => 'return multipleDelete("comment-grid",this.href, "Comment")')
    );
}

Helper::cs()->registerScriptFile(Helper::themeUrl() . '/js/multi-delete.js', CClientScript::POS_END);